//
//  ICNickNameController.m
//  ICome
//
//  Created by ENN on 16/8/11.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICNickNameController.h"
#import "ICTextField.h"

@interface ICNickNameController ()

@property (nonatomic, strong) UIButton *rightBtn;
@property (nonatomic, weak) ICTextField * textF;

@end

@implementation ICNickNameController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self setupNav];
}


- (void)setupNav
{
    self.title = @"修改昵称";
    
//    UIButton *btn       = [UIButton buttonWithType:UIButtonTypeCustom];
//    [btn setTitle:@"完成" forState:UIControlStateNormal];
//    btn.titleLabel.font = [UIFont systemFontOfSize:17.0];
//    [btn setTitleColor:ICRGB(0x954316) forState:UIControlStateNormal];
//    btn.width  = 50;
//    btn.height = 50;
//    [btn addTarget:self action:@selector(changeNickName) forControlEvents:UIControlEventTouchUpInside];
//    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
//    btn.enabled = NO;
//    [self.navigationItem setRightBarButtonItem:barItem];
//    [btn setTitleColor:DEFAULT_SUBJECT_COLOR forState:UIControlStateNormal];
//    self.rightBtn              = btn;
    
    _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _rightBtn.enabled = NO;
    _rightBtn.frame = CGRectMake(0, 0, 44., 44.);
    [_rightBtn setTitle:@"完成" forState:UIControlStateNormal];
    [_rightBtn setTitleColor:ICRGB(0xffffff) forState:UIControlStateNormal];
    _rightBtn.alpha = 0.5;
    [_rightBtn addTarget:self action:@selector(changeNickName) forControlEvents:UIControlEventTouchUpInside];
    _rightBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    _rightBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 0);
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_rightBtn];
    
}


- (void)setupUI
{
    self.view.backgroundColor = IColor(240, 237, 237);
    UILabel *titleLabel  = [[UILabel alloc] init];
    titleLabel.text      = @"我在本会话中的昵称";
    titleLabel.font      = [UIFont systemFontOfSize:14];
    titleLabel.textColor = ICRGB(0x9a9a9a);
    [self.view addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(14);
        make.centerY.equalTo(self.view.mas_top).offset(18);
    }];
    
    UIView *bgView      = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.frame        = CGRectMake(0, 36, self.view.width, 41);
    [self.view addSubview:bgView];
    ICTextField * textF = [[ICTextField alloc] initWithFrame:CGRectMake(14, 0, bgView.width-14, bgView.height)];
    self.textF          = textF;
    [bgView addSubview:textF];
    
    NSString *nickName = [ICMessageDatabase nickNameWithEid:[ICUser currentUser].eId gId:_group.gId];
    if (nickName) {
        textF.text = nickName;
    } else {
        textF.text = [ICMessageDatabase getUserNameWithEId:[ICUser currentUser].eId];
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldChanged) name:UITextFieldTextDidChangeNotification object:nil];
    
}

- (void)changeNickName
{
    if (self.textF.text.length == 0) return;
    if ([self.textF.text isEqualToString:[ICMessageDatabase nickNameWithEid:[ICUser currentUser].eId gId:_group.gId]]) {
        [self.navigationController popViewControllerAnimated:YES];
        return;
    }
    
    if ([ICTools charNumber:self.textF.text] > 20) {
        [MBProgressHUD showError:@"昵称限制在20个字符以内"];
        return;
    }
    [MBProgressHUD showMessage:@"正在修改..." toView:self.view];
    NSDictionary *param = @{
                            @"gId":self.group.gId,
                            @"nName":self.textF.text,
                            @"eId" :[ICUser currentUser].eId
                            };
    [[ICSocketRequestManager sharedInstance] changeNickNameWithObject:param Success:^(id object) {
        [MBProgressHUD hideHUDForView:self.view];
        [ICMessageDatabase updateNickNameWith:[ICUser currentUser].eId gId:_group.gId nName:self.textF.text];
        if ([self.delegate respondsToSelector:@selector(changedNickName)]) {
            [self.delegate changedNickName];
        }
        [self.navigationController popViewControllerAnimated:YES];
    } Failure:^(NSError *error, id other) {
        [MBProgressHUD hideHUDForView:self.view];
        [MBProgressHUD showError:@"修改失败"];
    }];
}




#pragma mark - UITextFieldDelegate

- (void)textFieldChanged
{
    if (self.textF.text.length > 0) {
        self.rightBtn.enabled = YES;
        self.rightBtn.alpha = 1;
        [self.rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    } else {
        self.rightBtn.enabled = NO;
        self.rightBtn.alpha = 0.5;
        [self.rightBtn setTitleColor:ICRGB(0xffffff) forState:UIControlStateNormal];
    }
}




#pragma mark - Getter




@end
